package com.healthdata.service.impl;

import com.healthdata.dao.ComprehensiveHospitalCostMapper;
import com.healthdata.entity.ComprehensiveHospitalCost;
import com.healthdata.service.ComprehensiveHospitalCostService;
import com.healthdata.vo.MedicalCostRatioVO;
import com.healthdata.vo.MedicalCostTrendVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ComprehensiveHospitalCostServiceImpl implements ComprehensiveHospitalCostService {

    @Autowired
    private ComprehensiveHospitalCostMapper costMapper;

    @Override
    public List<ComprehensiveHospitalCost> getAllCostData() {
        return costMapper.findAll();
    }

    private MedicalCostTrendVO.CostData convertToCostData(ComprehensiveHospitalCost cost) {
        MedicalCostTrendVO.CostData costData = new MedicalCostTrendVO.CostData();
        costData.setId(cost.getId());
        costData.setLevel(cost.getLevel());
        costData.setYear(cost.getYear());
        costData.setHospitalTotalCost(cost.getHospitalTotalCost());
        costData.setMedicineFee(cost.getMedicineFee());
        costData.setInspectionFee(cost.getInspectionFee());
        costData.setMedicineRatio(cost.getMedicineRatio());
        costData.setInspectionRatio(cost.getInspectionRatio());
        return costData;
    }
    @Override
    public MedicalCostTrendVO getMedicalCostTrend() {
        // 修正：使用注入的mapper实例调用方法
        // 注意：需要确保Mapper接口中有正确的方法签名
        List<ComprehensiveHospitalCost> costData = costMapper.findByYear(2015); // 假设年份固定为2015

        List<MedicalCostTrendVO.CostData> costDataVOs = costData.stream()
                .map(this::convertToCostData)
                .collect(Collectors.toList());

        MedicalCostTrendVO trendVO = new MedicalCostTrendVO();
        trendVO.setCostData(costDataVOs);
        return trendVO;
    }

    @Override
    public MedicalCostRatioVO getMedicalCostRatio() {
        MedicalCostRatioVO vo = new MedicalCostRatioVO();

        // 获取所有年份
        List<Integer> years = costMapper.findAllYears();
        vo.setYears(years);

        // 获取各级别比率数据
        vo.setCommittee(getRatioDataByLevel("委属", years));
        vo.setProvince(getRatioDataByLevel("省属", years));
        vo.setPrefectureLevel(getRatioDataByLevel("地级市属", years));
        vo.setCountyLevel(getRatioDataByLevel("县级市属", years));
        vo.setCounty(getRatioDataByLevel("县属", years));

        return vo;
    }

    @Override
    public List<ComprehensiveHospitalCost> getCostDataByLevelAndYear(String level, List<Integer> years) {
        return costMapper.findByLevelAndYear(level, years);
    }

    @Override
    public List<Integer> getAllYears() {
        return costMapper.findAllYears();
    }

    @Override
    public List<String> getAllLevels() {
        return costMapper.findAllLevels();
    }

    /**
     * 按级别获取费用数据
     */
    private List<MedicalCostTrendVO.CostData> getCostDataByLevel(String level, List<Integer> years) {
        return years.stream()
                .map(year -> {
                    List<ComprehensiveHospitalCost> costDataList = costMapper.findCostDataByYear(level, year);
                    if (!costDataList.isEmpty()) {
                        ComprehensiveHospitalCost entity = costDataList.get(0);
                        MedicalCostTrendVO.CostData data = new MedicalCostTrendVO.CostData();
                        // 手动转换实体到VO
                        data.setTotalCost(entity.getHospitalTotalCost());
                        data.setMedicineFee(entity.getMedicineFee());
                        data.setInspectionFee(entity.getInspectionFee());
                        data.setMedicineRatio(entity.getMedicineRatio());
                        data.setInspectionRatio(entity.getInspectionRatio());
                        return data;
                    }
                    return new MedicalCostTrendVO.CostData(); // 返回空对象
                })
                .collect(Collectors.toList());
    }

    /**
     * 按级别获取比率数据
     */
    private List<MedicalCostRatioVO.RatioData> getRatioDataByLevel(String level, List<Integer> years) {
        return years.stream()
                .map(year -> {
                    // 假设 findCostDataByYear 返回的是 ComprehensiveHospitalCost 列表
                    List<ComprehensiveHospitalCost> costDataList = costMapper.findCostDataByYear(level, year);
                    MedicalCostRatioVO.RatioData data = new MedicalCostRatioVO.RatioData();
                    if (!costDataList.isEmpty()) {
                        ComprehensiveHospitalCost entity = costDataList.get(0);
                        data.setMedicineRatio(entity.getMedicineRatio());
                        data.setInspectionRatio(entity.getInspectionRatio());
                    }
                    return data;
                })
                .collect(Collectors.toList());
    }
}